<template>
  <view class="EnrollDetail">
    <cu-form ref="profileForm">

      <view class="form-group">
        <cu-input disabled name="STUDENTNAME" title="真实姓名" placeholder="暂无信息"/>
      </view>
      <view class="form-group">
        <cu-input disabled type="number" name="STUDENTNO" title="学号" placeholder="暂无信息"/>
      </view>

      <view class="form-group">
        <cu-input disabled type="idcard" name="SFZNO" title="身份证号" placeholder="暂无信息"/>
      </view>

      <view class="form-group">
        <cu-input disabled name="PHONENO" title="联系电话" placeholder="暂无信息"/>
      </view>

      <view class="form-group" style="border-bottom: 2rpx #e2e2e2 solid;">
        <cu-input disabled name="ADDR" title="现住址" placeholder="暂无信息"/>
      </view>
      <view class="form-group">
        <cu-radio disabled name="SEX" title="性别" :options="sexOpt"/>
      </view>

      <view class="margin"></view>

      <view class="form-group">
        <cu-input disabled name="LXR" title="紧急联系人" placeholder="暂无信息"/>
      </view>

      <view class="form-group">
        <cu-input disabled type="number" name="LXR_PHONENO" title="紧急联系人电话" placeholder="暂无信息"/>
      </view>

      <view class="margin"></view>

      <view class="form-group">
        <cu-picker disabled name="ZZMM" title="政治面貌" :options="ZZMMOpts"/>
      </view>

      <view class="form-group">
        <cu-picker disabled name="WHCD" title="文化程度" :options="WHCDOpts"/>
      </view>

      <view class="margin"></view>

      <view class="form-group">
        <cu-input disabled name="YDW" title="原单位" placeholder="暂无信息"/>
      </view>

      <view class="form-group">
        <cu-input disabled name="YZW" title="原职位" placeholder="暂无信息"/>
      </view>

      <view class="margin"></view>

      <view class="form-group">
        <cu-uploader disabled name="PICID" title="个人照片"/>
      </view>

      <view class="form-group">
        <cu-uploader disabled name="ZKCNSID" title="健康安全承诺书"/>
      </view>

      <view class="margin"></view>
	
      <view class="form-group" v-if="enrollData.CLASSNAME">
        <view class="bg-white padding">
          <view class="title">当前班级名称：</view>
          <view class="text-sm text-black margin-top-sm">当前班级名称:{{enrollData.CLASSNAME }}</view>
		</view>
      </view>
	  <view class="form-group" v-if="enrollData.Y_CLASSNAME">
	    <view class="bg-white padding">
	      <view class="title">原班级名称：</view>
	      <view class="text-sm text-black margin-top-sm"> 原班级名称：{{enrollData.Y_CLASSNAME}}</view>
	  		</view>
	  </view>
	  <view class="form-group" v-if="enrollData.Z_CLASSNAME">
	    <view class="bg-white padding">
	      <view class="title">要转班级名称：</view>
	      <view class="text-sm text-black margin-top-sm">要转班级名称：{{enrollData.Z_CLASSNAME?enrollData.Z_CLASSNAME:"无"}}</view>
	  		</view>
	  </view>
	  <view class="form-group" v-if="enrollData.STUDENT_FLAGNR" style="border-bottom: 2rpx #e2e2e2 solid;">
	    <cu-input disabled name="STUDENT_FLAGNR" title="学生性质" placeholder="暂无信息"/>
	  </view>
	  <view class="bg-white padding" style="border-bottom: 2rpx #e2e2e2 solid;" v-if="type==='apply'">
	  		<text class="margin-right" style="color: black;">审核状态</text><text>{{enrollData.SH_FLAGNR}}</text>
	  </view>
	  <view class="bg-white padding" style="border-bottom: 2rpx #e2e2e2 solid;" v-if="type==='apply'"> 
	  		<text class="margin-right" style="color: black;">审核结果</text><text>{{enrollData.STATENR}}</text>
	  </view>
    </cu-form>
    <view class="cu-bar btn-group margin-top" v-if="type === 'verify'||type === 'bmb'">
      <button class="cu-btn bg-green shadow-blur" @tap="verifyEnroll('1')">通过</button>
      <button class="cu-btn bg-red shadow-blur" @tap="verifyEnroll('2')">驳回</button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import CuInput from '../../components/cu-input/index.vue'
import CuRadio from '../../components/cu-radio/index.vue'
import CuPicker from '../../components/cu-picker/index.vue'
import CuForm from '../../components/cu-form/index.vue'
import CuUploader from '../../components/cu-uploader/index.vue'
import {useStore} from "vuex";
import {usePreview} from "../../utils/usePreview";
import {useProfileArgs} from "../profile/useProfileArgs";
import {nextTick, onMounted, reactive, Ref, ref, watchEffect} from "vue";
import {onLoad} from "@dcloudio/uni-app";
import {EnrollListDAT, UserEnrollListDAT, VerifyEnrollDetailDAT} from "../../types/responseTypes";
import {ReactiveVariable} from "vue/macros";
import {doVerifyEnroll, doVerifyZZYEnroll} from "../../api/enroll";
import {RefundAuditLast} from "../../api/refundaudit"
const profileForm = ref<any>(null)
const {CUSER_ID, USERNO, USERNAME} = useStore().state.user.userInfo
const {previewImage} = usePreview()
const {ZZMMOpts, WHCDOpts, sexOpt} = useProfileArgs([
  {
    CSNAME: '政治面貌',
    key: 'ZZMMOpts'
  },
  {
    CSNAME: '文化程度',
    key: 'WHCDOpts'
  },
  {
    CSNAME: '性别',
    key: 'sexOpt'
  }
]).opts

let enrollData = ref<any>({}) as ReactiveVariable<VerifyEnrollDetailDAT | UserEnrollListDAT>
const type = ref<any>('bmb')
const {state} = useStore()
const isZZY = ref(false)

 function verifyEnroll(SH_FLAG: '1' | '2') {
	 uni.showModal({
		title: '',
		content: '确认提交审核吗',
		success: reslove => {
			if(reslove.confirm){
				submit(SH_FLAG)
			}
		},
		fail: () => {},
		complete: () => {}
	});
}

async function submit(SH_FLAG: '1' | '2'){
		try {
		  const res = isZZY.value ? await doVerifyZZYEnroll({
		    CBM_ID: String((enrollData.value as VerifyEnrollDetailDAT).CBM_ID),
		    SH_FLAG,
		    OPERATOR_ID: state.user.userInfo.CUSER_ID
		  }) : await doVerifyEnroll({
		    CBM_ID: String((enrollData.value as VerifyEnrollDetailDAT).CBM_ID),
		    SH_FLAG,
		    OPERATOR_ID: state.user.userInfo.CUSER_ID
		  })
		  if (res.ERROR[0].errcode == '0') {
		    uni.showToast({
		      title: '审核成功',
		      icon: 'success',
		      mask: true
		    })
				if(res.COUNT[0].total_fee!=null&&res.COUNT[0].order_code!=null&&res.COUNT[0].transaction_id!=null){
				  RefundAuditLast({
					total_fee:res.COUNT[0].total_fee,
					order_code:res.COUNT[0].order_code,
					transaction_id:res.COUNT[0].transaction_id,
                    OPERATOR_ID: state.user.userInfo.CUSER_ID
				  })
				  .then(res=>{
					if (res.result_code == "SUCCESS"){
					  uni.showToast({
						  title: "审核成功",
						  icon: 'none',
						  mask: true
					  })
						setTimeout(() => {
						  uni.navigateBack({
							success: () => {
							  uni.$emit('finishVerify')
							}
						  })
						}, 500)
					}else {
						uni.showToast({
								title: res.ERROR[0].error,
								icon: 'none',
								mask: true
						})
					}
				  })
				  .catch(err=>{
					uni.showToast({
						title: '审核失败',
						icon: 'error',
						mask: true
					})
				  })
			  }else{
				  setTimeout(()=>{
				  	uni.navigateBack({
				  		delta:1
				  	})
				  },500)
			  }
		  } else {
		    uni.showToast({
		      title: res.ERROR[0].error,
		      icon: 'none',
		      mask: true
		    })
		  }
		} catch (err) {
		  uni.showToast({
		    title: '审核失败',
		    icon: 'error',
		    mask: true
		  })
		}
}
const props = withDefaults(defineProps<{
  detail: UserEnrollListDAT
}>(), {})


onLoad((options: any) => {
  if (options.info) {
	 enrollData.value = JSON.parse(decodeURIComponent(options.info))
  }
  if (options.type) {
    type.value = options.type
  }

  if (options.ZZY == '1') {
    isZZY.value = true
  }
  if(type.value === "apply"){
	  uni.setNavigationBarTitle({
	    title: "报名表详情"
	  })
  }else{
	  uni.setNavigationBarTitle({
	    title: type.value === 'verify' ? '转专业审核' : '报名表审核'
	  })
  }
  
})

watchEffect(() => {
  if (props.detail){
    enrollData.value = props.detail
  }
  if (profileForm.value){
    nextTick(() => {
      profileForm.value.setFields(['STUDENTNAME', 'STUDENTNO', 'PICID', 'ZKCNSID', 'SFZNO', 'SEX', 'ADDR', 'PHONENO', 'LXR', 'LXR_PHONENO', 'ZZMM', 'WHCD', 'YDW', 'YZW','STUDENT_FLAGNR'].map(name => ({
        value: ((enrollData.value as any)[name] || '') + '',
        name
      })))
    })
  }

})
</script>

<style scoped lang="scss">
.EnrollDetail {
  box-sizing: border-box;
  padding-bottom: constant(safe-area-inset-bottom);  ///兼容 IOS<11.2/
  padding-bottom: env(safe-area-inset-bottom);  ///兼容 IOS>11.2/
  .form-group+.form-group{
    border-top: 1upx solid #eee;
  }
}
</style>
